package com.jbm.jbmsupplier.activity.register.presenter

import android.content.Context
import android.content.Intent
import android.os.CountDownTimer
import android.view.View
import com.jbm.core.common.MainServer
import com.jbm.enterapplication.InterfaceMa
import com.jbm.enterapplication.activityhome.view.OpenDoor
import com.jbm.enterapplication.sharedprefer.SharedPreActivity
import com.jbm.enterapplication.utlisclass.ExitActivity
import com.jbm.enterapplication.view.DialogLoading
import com.jbm.enterapplication.view.DoubleButtonDialog
import com.jbm.jbmsupplier.activity.register.mode.Regiter_Mode_User_WX
import com.jbm.jbmsupplier.activity.register.view.Register_Construction_Massages
import com.jbm.onehome.common.OHInterface

/**
 * Created by 家帮忙 on 2017/8/23.
 */
class Register_Present_WX : Register_Present_Interface_WX {

    var loginid:String?=null
    var loginids:String?=null
    var regitermodeusercon = Regiter_Mode_User_WX();
    var register_construction_massagtes: Register_Construction_Massages? = null
     var context:Context?=null
    var shandpreftr:SharedPreActivity?=null
    constructor(register_construction_massagtes: Register_Construction_Massages?,content: Context) {
        this.register_construction_massagtes = register_construction_massagtes
        this.context=content
        shandpreftr= SharedPreActivity(context)
    }

     var  dialogLoading: DialogLoading?=null

    override fun getRegisterUserMassage(content: Context) {//注册施工段
        if (register_construction_massagtes!!.getPhone().equals("")){
            register_construction_massagtes!!.getPhoneErr().error = "手机号不能为空"
            return
        }else if(register_construction_massagtes!!.getPhone().length<11){
            register_construction_massagtes!!.getPhoneErr().error = "手机号长度不符"
            return
        }else if(register_construction_massagtes!!.getValidateCode().equals("")){
            register_construction_massagtes!!.getValidateCodeErr().error = "验证码不能为空"
            return
        }
// else if(register_construction_massagtes!!.getPasWord().equals("")){
//            register_construction_massagtes!!.getPasWordErr().error = "密码不能为空"
//            return
//        }
 else{
            dialogLoading = DialogLoading(context!!, "加载中.....", false)
            dialogLoading?.show()
        regitermodeusercon.GainRegisterIntergace(register_construction_massagtes!!.getPhone(), register_construction_massagtes!!.getValidateCode(), shandpreftr?.getLoginId()!!
                , shandpreftr?.getWXsex()!!, shandpreftr?.getWXname()!!, shandpreftr?.getWXhand()!!,object :InterfaceMa<String>{
            override fun onSuccess(t: String, boolean: Boolean) {
                if (boolean==true){
                    setDialogding()
                    shandpreftr?.setIsUser(shandpreftr?.getLoginId()!!, "")//保存账号密码
                    shandpreftr?.setIsConn(true)//连接网络成功
                    shandpreftr?.setIsLogin(true)//登录成功
                    shandpreftr?.setLoginId(OHInterface.getG_server().login_id.toString())//保存登录id
                    MainServer.getG_server().m_local_id = OHInterface.getG_server().login_id
                    context?.startActivity(Intent(context, OpenDoor::class.java))
                    register_construction_massagtes?.finlis()
                    ExitActivity.getInstanst().exitActivity()
                }


            }

            override fun setError(err: String, boolean: Boolean) {
                if (boolean==false){
                    setDialogding()
                    hint(err)
                }
            }

        })
    }}

     var  mcount: MyCount?=null

    override fun getValidateCode() {//获取验证码
        if(register_construction_massagtes!!.getPhone().equals("")){
            register_construction_massagtes!!.getPhoneErr().error = "验证码不能为空"
            return
        }else{
            dialogLoading = DialogLoading(context!!, "加载中.....", false)
            dialogLoading?.show()
            mcount = MyCount(90000, 1000)
            mcount?.start()
        regitermodeusercon.GainValidateCode(register_construction_massagtes!!.getPhone(),object :InterfaceMa<String>{
            override fun onSuccess(t: String, boolean: Boolean) {
               if (boolean==true){
                   setDialogding()
                   if (mcount != null) {
                       register_construction_massagtes?.getValidateCodes()?.text="发送验证码"
                       mcount?.cancel()
                   }
               }
            }

            override fun setError(err: String, boolean: Boolean) {
                if (boolean==false){
                    setDialogding()
                    hint(err)
                    if (mcount != null) {
                        register_construction_massagtes?.getValidateCodes()?.text="发送验证码"
                        mcount?.cancel()
                    }
                }
            }

        })}
    }









    fun setDialogding() {
        if (dialogLoading != null) {
            dialogLoading!!.dismiss()
        }
    }

    //一些提示的时候调用
    fun hint(name: String) {
        val dialog = DoubleButtonDialog(context, name)
        dialog.getokbtn().setOnClickListener(View.OnClickListener { dialog.dismiss() })
        dialog.setisVisibility()

    }


    /*定义一个倒计时的内部类*/
    inner class MyCount(millisInFuture: Long, countDownInterval: Long) : CountDownTimer(millisInFuture, countDownInterval) {

        override fun onFinish() {
            register_construction_massagtes?.getValidateCodes()?.text="发送验证码"

        }

        override fun onTick(millisUntilFinished: Long) {
            register_construction_massagtes?.getValidateCodes()?.text="请等待90秒(" + millisUntilFinished / 1000 + ")..."

        }
    }


}